package com.ycuwq.datepicker.date;

import com.ycuwq.datepicker.WheelPicker;
import com.ycuwq.datepicker.util.AttrUtils;
import ohos.agp.components.AttrSet;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 年份选择器
 * Created by ycuwq on 17-12-27.
 */
@SuppressWarnings("unused")
public class YearPicker extends WheelPicker<Integer> {
    /**
     * * The constant YearPicker_startYear
     */
    public static final String YEARPICKER_STARTYEAR = "startYear";
    /**
     * * The constant YearPicker_endYear
     */
    public static final String YEARPICKER_ENDYEAR = "endYear";
    /**
     * The constant M start year
     */
    private int mStartYear;
    /**
     * M end year
     */
    private int mEndYear;
    /**
     * The constant M selected year
     */
    private int mSelectedYear;
    /**
     * The constant M on year selected listener
     */
    private OnYearSelectedListener mOnYearSelectedListener;

    /**
     * Year picker
     *
     * @param context context
     */
    public YearPicker(Context context) {
        this(context, null);
    }

    /**
     * Year picker
     *
     * @param context context
     * @param attrs   attrs
     */
    public YearPicker(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    /**
     * Year picker
     *
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr def style attr
     */
    public YearPicker(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        setItemMaximumWidthText("0000");
        updateYear();
        setSelectedYear(mSelectedYear, false);
        setOnWheelChangeListener(new OnWheelChangeListener<Integer>() {
            @Override
            public void onWheelSelected(Integer item, int position) {
                mSelectedYear = item;
                if (mOnYearSelectedListener != null) {
                    mOnYearSelectedListener.onYearSelected(item);
                }
            }
        });
    }

    /**
     * Init attrs *
     *
     * @param context context
     * @param attrs   attrs
     */
    private void initAttrs(Context context, AttrSet attrs) {
        if (attrs == null) {
            return;
        }

        mSelectedYear = Calendar.getInstance().get(Calendar.YEAR);
        mStartYear = AttrUtils.getIntFromAttr(attrs, YEARPICKER_STARTYEAR, 1900);
        mEndYear = AttrUtils.getIntFromAttr(attrs, YEARPICKER_ENDYEAR, 2100);
    }

    /**
     * Update year
     */
    private void updateYear() {
        List<Integer> list = new ArrayList<>();
        for (int i = mStartYear; i <= mEndYear; i++) {
            list.add(i);
        }
        setDataList(list);
    }

    /**
     * Set start year *
     *
     * @param startYear start year
     */
    public void setStartYear(int startYear) {
        mStartYear = startYear;
        updateYear();
        if (mStartYear > mSelectedYear) {
            setSelectedYear(mStartYear, false);
        } else {
            setSelectedYear(mSelectedYear, false);
        }
    }

    /**
     * Set end year *
     *
     * @param endYear end year
     */
    public void setEndYear(int endYear) {
        mEndYear = endYear;
        updateYear();
        if (mSelectedYear > endYear) {
            setSelectedYear(mEndYear, false);
        } else {
            setSelectedYear(mSelectedYear, false);
        }
    }

    /**
     * Set year *
     *
     * @param startYear start year
     * @param endYear   end year
     */
    public void setYear(int startYear, int endYear) {
        setStartYear(startYear);
        setEndYear(endYear);
    }

    /**
     * Set selected year *
     *
     * @param selectedYear selected year
     * @param smoothScroll smooth scroll
     */
    public void setSelectedYear(int selectedYear, boolean smoothScroll) {
        setCurrentPosition(selectedYear - mStartYear, smoothScroll);
    }

    /**
     * Get selected year int
     *
     * @return the int
     */
    public int getSelectedYear() {
        return mSelectedYear;
    }

    /**
     * Set selected year *
     *
     * @param selectedYear selected year
     */
    public void setSelectedYear(int selectedYear) {
        setSelectedYear(selectedYear, true);
    }

    /**
     * Set on year selected listener *
     *
     * @param onYearSelectedListener on year selected listener
     */
    public void setOnYearSelectedListener(OnYearSelectedListener onYearSelectedListener) {
        mOnYearSelectedListener = onYearSelectedListener;
    }

    /**
     * On year selected listener
     */
    public interface OnYearSelectedListener {
        /**
         * On year selected *
         *
         * @param year year
         */
        void onYearSelected(int year);
    }

}
